﻿using System;
using System.Collections.Generic;
using System.Text;

namespace zielonka.co.uk.extensions.system.collections.hashtable
{
    // Description:	Upgrades a hashtable to a generic dictionary
    /*
         Hashtable t1 = new Hashtable();
         t1.Add("pi", Math.PI);
         t1.Add("e", Math.E);

         var col = t1.Upgrade<string, double>();
    */

    public static partial class HashtableExtensions
    {
        public static string ToGenericDictionary<T>(this IEnumerable<T> collection, string separator)
        {
            return ToString(collection, t => t.ToString(), separator);
        }

        public static string ToString<T>(this IEnumerable<T> collection, Func<T, string> stringElement, string separator)
        {
            StringBuilder sb = new StringBuilder();
            foreach (var item in collection)
            {
                sb.Append(stringElement(item));
                sb.Append(separator);
            }
            return sb.ToString(0, Math.Max(0, sb.Length - separator.Length));  // quita el ultimo separador
        }
    }
}
